package xml;


import javax.xml.bind.annotation.adapters.XmlAdapter;

import cuShapes.Box;
import cuShapes.Connectable;
import cuShapes.PQShape;

public class ConnectableAdapter extends XmlAdapter<Integer, Connectable> {
	// adapt original Java construct to a type
	// which we can easily map to the XML output we want
	public Integer marshal(Connectable c) throws Exception {
		int id = c.getShapeId();
		return id;
	}

	// reverse operation: map XML type to Java
	public Connectable unmarshal(Integer id) throws Exception { 
		// Box is a dummy object just for containing shapeId
		// I am using dummy objects so that the xml file only has to store a Connectables
		// shapeId, otherwise the entire shape's data would be saved multiple times.
		// Once the xml file is being read back in I need a temporary place(the box dummy object) for
		// the shapeId to be stored before I call reAddConnections on each shape in shapeList.
		// reAddConnections cannot be called immediately because some of the shapes that may
		// be connected have yet to be created.
		PQShape newShape = new Box();
		newShape.setShapeId(id);
		
		return (Connectable) newShape;
	}
}
